package com.maserb.monitor.web.system_manage;

import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.DataDictionaryEntity;
import com.maserb.monitor.entity.DataDictionaryItemEntity;
import com.maserb.monitor.service.system_manage.DataDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/dataDictionary")
public class DataDictionaryController {

    @Autowired
    DataDictionaryService dataDictionaryService;

    @RequestMapping(value = {"/", "/index"})
    public String getIndex() {
        return "system_manage/data_dictionary/index";
    }

    @RequestMapping("/getEditDataDictionary/{id}")
    public ModelAndView getEditDataDictionary(@PathVariable final String id) {
        ModelAndView result = new ModelAndView();
        DataDictionaryEntity entity = dataDictionaryService.findOne(id);
        result.setViewName("system_manage/data_dictionary/edit");
        result.addObject("entity", entity);
        List<String> values = entity.getItems().stream().map(DataDictionaryItemEntity::getName).collect(Collectors.toList());
        result.addObject("values", StringUtils.join(values, ","));
        return result;
    }

    @RequestMapping("/doUpdateDataDictionary")
    @ResponseBody
    public AjaxResult<Boolean> doUpdateDataDictionary(final HttpServletRequest request, DataDictionaryEntity entity, String values) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                saveDataDictionary(entity, values);
                return true;
            }
        });
    }

    @RequestMapping("/getAddDataDictionary")
    public ModelAndView getAddDataDictionary() {
        ModelAndView result = new ModelAndView();
        result.setViewName("system_manage/data_dictionary/add");
        return result;
    }

    @RequestMapping("/doListDataDictionary")
    @ResponseBody
    public PageResponseResult<DataDictionaryEntity> doListDataDictionary(PageRequestParam page, final String name) {
        return AjaxUtils.processPage(page, new Func_T1<Pageable, Page<DataDictionaryEntity>>() {
            @Override
            public Page<DataDictionaryEntity> invoke(Pageable var1) throws Exception {
                Page<DataDictionaryEntity> page = dataDictionaryService.findAll(var1, name);
                return page;
            }
        });
    }

    @RequestMapping("/doAddDataDictionary")
    @ResponseBody
    public AjaxResult<Boolean> doAddDataDictionary(final HttpServletRequest request, DataDictionaryEntity entity, String values) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                saveDataDictionary(entity, values);
                return true;
            }
        });
    }

    @RequestMapping("/doDeleteDataDictionary")
    @ResponseBody
    public AjaxResult<Boolean> doDeleteDataDictionary(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
               dataDictionaryService.deleteAllByKeys(keys);
                return true;
            }
        });
    }

    private void getDataDictionaryEntity(String[] valueArray, List<DataDictionaryItemEntity> itemList, DataDictionaryEntity entity) {
        if (valueArray.length > 0) {
            for (String str : valueArray) {
                DataDictionaryItemEntity item = new DataDictionaryItemEntity();
                item.setName(str);
                itemList.add(item);
            }
            entity.setItems(itemList);
        }
    }

    private void saveDataDictionary(DataDictionaryEntity entity, String values) {
        String[] valueArray = values.split(",");
        entity.setMode(1);
        List<DataDictionaryItemEntity> itemList = new ArrayList<>();
        getDataDictionaryEntity(valueArray, itemList, entity);

        entity.setCreateTime(CreateTimeUtils.getCreateTime());
        dataDictionaryService.save(entity, itemList);
    }
}